//
//  shenheController.m
//  pacc
//
//  Created by 测试 on 2018/6/1.
//  Copyright © 2018年 测试. All rights reserved.
//

#import "shenheController.h"
#import "SlideBarCell.h"
#import "searchController.h"
#import "orderModel.h"
#import "ZZcommonModel.h"
#import "orderModel.h"
#import "taskDetailsCVc.h"
#import "yuepianController.h"
#import "shenhetuihuiController.h"

#define TOPHEIGHT 45
@interface shenheController ()<UITableViewDelegate,UITableViewDataSource,NSFetchedResultsControllerDelegate>
@property (nonatomic, strong) NSManagedObjectContext *companyMOC;
@property (strong, nonatomic)  UITableView *myTableView1;
@property (strong, nonatomic)  UITableView *myTableView2;
@property (strong, nonatomic)  UITableView *myTableView;
@property(nonatomic,strong)    UIImageView * myImageView;
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultController;
@property (assign) BOOL isFrash;
@property (assign) int pageNo1;
@property (assign) int pageNo2;
@property (assign) int pageNo3;
///@brife TableViews的数据源
@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) NSMutableArray *dataSource1;
@property (strong, nonatomic) NSMutableArray *dataSource2;
@property (strong, nonatomic) NSMutableArray *dataSource3;
@property (strong, nonatomic) NSMutableArray *dataSource4;
@property (strong, nonatomic) NSMutableArray *dataSource5;
@property (strong, nonatomic) NSMutableArray *dataSource6;
@property (copy, nonatomic) NSString *nameStr;
@property (assign) NSInteger tabCount;
@property (strong, nonatomic) NSMutableArray *btnNameArr;

///@brife 整个视图的大小
@property (assign) CGRect mViewFrame;

///@brife 下方的ScrollView
@property (strong, nonatomic) UIScrollView *scrollView;

///@brife 上方的按钮数组
@property (strong, nonatomic) NSMutableArray *topViews;

///@brife 下方的表格数组
@property (strong, nonatomic) NSMutableArray *scrollTableViews;


///@brife 当前选中页数
@property (assign) NSInteger currentPage;

///@brife 下面滑动的View
@property (strong, nonatomic) UIView *slideView;

///@brife 上方的view
@property (strong, nonatomic) UIView *topMainView;

///@brife 上方的ScrollView
@property (strong, nonatomic) UIScrollView *topScrollView;

@end

@implementation shenheController

#pragma mark -- 初始化表格的数据源
-(void) initDataSource{
    _dataSource = [[NSMutableArray alloc] initWithCapacity:_tabCount];
    
    for (int i = 1; i <= _tabCount; i ++) {
        
        NSMutableArray *tempArray  = [[NSMutableArray alloc] initWithCapacity:5];
        
        for (int j = 1; j <= 9; j ++) {
            
            NSString *tempStr = [NSString stringWithFormat:@"我是第%d个TableView的第%d条数据。", i, j];
            [tempArray addObject:tempStr];
        }
        
        [_dataSource addObject:tempArray];
    }
}


#pragma mark -- 实例化ScrollView
-(void) initScrollView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - TOPHEIGHT)];
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * _tabCount, 0);
    _scrollView.backgroundColor = baseBackgroundColor;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.alwaysBounceVertical = NO;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
}

#pragma mark -- 实例化顶部的tab
-(void) initTopTabs{
    
    if (!_btnNameArr) {
        _btnNameArr = [[NSMutableArray alloc]initWithCapacity:_tabCount];
    }
    
    CGFloat width = _mViewFrame.size.width / 6;
    
    if(self.tabCount <=6){
        width = _mViewFrame.size.width / self.tabCount;
    }
    
    _topMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mViewFrame.size.width, TOPHEIGHT)];
    
    _topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _mViewFrame.size.width, TOPHEIGHT)];
    
    _topScrollView.showsHorizontalScrollIndicator = NO;
    
    _topScrollView.showsVerticalScrollIndicator = YES;
    
    _topScrollView.bounces = NO;
    
    _topScrollView.delegate = self;
    
    if (_tabCount >= 6) {
        _topScrollView.contentSize = CGSizeMake(width * _tabCount, TOPHEIGHT);
        
    } else {
        _topScrollView.contentSize = CGSizeMake(_mViewFrame.size.width, TOPHEIGHT);
    }
    
    
    [self.view addSubview:_topMainView];
    
    [_topMainView addSubview:_topScrollView];
    
    
    
    for (int i = 0; i < _btnNameArr.count; i ++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TOPHEIGHT)];
        
        view.backgroundColor = [UIColor whiteColor];
        
        //        if (i % 2) {
        //            //            view.backgroundColor = [UIColor grayColor];
        //        }
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, TOPHEIGHT)];
        button.tag = i;
        [button setTitle:[NSString stringWithFormat:@"%@", _btnNameArr[i]] forState:UIControlStateNormal];
        [button setTitleColor:basetextColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = textfont;
        [view addSubview:button];
        
        UIView *vview = [[UIView alloc] initWithFrame:CGRectMake(width-1,ZTmargin*2, 1,TOPHEIGHT-ZTmargin*4)];
        [button addSubview:vview];
        vview.backgroundColor = ZXColor(208, 208, 208);
        
        [_topViews addObject:view];
        [_topScrollView addSubview:view];
    }
}
#pragma mark --点击顶部的按钮所触发的方法
-(void) tabButton: (id) sender{
    UIButton *button = sender;
    [_scrollView setContentOffset:CGPointMake(button.tag * _mViewFrame.size.width, 0) animated:YES];
    
    
}

#pragma mark --初始化下方的TableViews
-(void) initDownTables{
    for (int i = 0; i < 2; i ++) {
        UITableView * myTableView = [[UITableView alloc] initWithFrame:CGRectMake(i * _mViewFrame.size.width, ZTmargin + TOPHEIGHT, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT-64-48)];
        myTableView.backgroundColor = baseBackgroundColor;
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.tag = i;
        switch (i) {
            case 0:
                _myTableView1 = myTableView;
                break;
            case 1:
                _myTableView2 = myTableView;
                break;
            default:
                break;
        }
        myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_scrollTableViews addObject:myTableView];
        [_scrollView addSubview:myTableView];
    }
}
#pragma mark --refersh--
-(void)refersh{
        _pageNo1 =_pageNo2 =_pageNo3 = 1;
        _myTableView1.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            NSLog(@"刷新");
            [self loadData];
        }];
        _myTableView2.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            NSLog(@"刷新");
            [self loadData];
        }];
        _myTableView1.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadMoreData];
        }];
        _myTableView2.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadMoreData];
        }];
}
#pragma mark -- 初始化滑动的指示View
-(void) initSlideView{
    
    CGFloat width = _mViewFrame.size.width / 6;
    
    if(self.tabCount <=6){
        width = _mViewFrame.size.width / self.tabCount;
    }
    
    _slideView = [[UIView alloc] initWithFrame:CGRectMake(0, TOPHEIGHT - 1, width, 1)];
    [_slideView setBackgroundColor:titleBlueColor];
    [_topScrollView addSubview:_slideView];
}

#pragma mark --根据scrollView的滚动位置复用tableView，减少内存开支
-(void) updateTableWithPageNumber: (NSUInteger) pageNumber{
    [self loadData];
    [self changeBackColorWithPage:pageNumber];
    
    int tabviewTag = pageNumber % 2;
    
    CGRect tableNewFrame = CGRectMake(pageNumber * _mViewFrame.size.width, TOPHEIGHT, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT-64-48);
    
    UITableView *reuseTableView = _scrollTableViews[tabviewTag];
    reuseTableView.frame = tableNewFrame;
    //切换数据
    //    [_dataSource removeAllObjects];
    //    for (int i = 1; i <= _tabCount; i ++) {
    //
    //        NSMutableArray *tempArray  = [[NSMutableArray alloc] initWithCapacity:20];
    //
    //        for (int j = 1; j <= 20; j ++) {
    //            int x = arc4random() % 100;
    //            NSString *tempStr = [NSString stringWithFormat:@"我是第haha%d个TableView的第%d条数据。", x, j];
    //            [tempArray addObject:tempStr];
    //        }
    //
    //        [_dataSource addObject:tempArray];
    //    }
    NSLog(@"切换tableView数据");
    [reuseTableView reloadData];
}


- (void) changeBackColorWithPage: (NSInteger) currentPage {
//    NSLog(@"_btnNameArr.count=%lu",(unsigned long)_btnNameArr.count);
//    NSLog(@"currentPage=%lu",(unsigned long)currentPage);
    for (int i = 0; i < _btnNameArr.count; i ++) {
        UIView *tempView = _topViews[i];
        UIButton *button = [tempView subviews][0];
        if (i == currentPage) {
            tempView.backgroundColor = [UIColor whiteColor];
            [button setTitleColor:titleBlueColor forState:UIControlStateNormal];
        } else {
            tempView.backgroundColor = [UIColor whiteColor];
            [button setTitleColor:baseGlayColor forState:UIControlStateNormal];
        }
    }
    [self.view setNeedsLayout];
}


#pragma mark -- scrollView的代理方法

-(void) modifyTopScrollViewPositiong: (UIScrollView *) scrollView{
    if ([_topScrollView isEqual:scrollView]) {
        CGFloat contentOffsetX = _topScrollView.contentOffset.x;
        
        CGFloat width = _slideView.frame.size.width;
        
        int count = (int)contentOffsetX/(int)width;
        
        CGFloat step = (int)contentOffsetX%(int)width;
        
        CGFloat sumStep = width * count;
        
        if (step > width/2) {
            
            sumStep = width * (count + 1);
            
        }
        
        [_topScrollView setContentOffset:CGPointMake(sumStep, 0) animated:YES];
        
        
        return;
    }
    
}

///拖拽后调用的方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //[self modifyTopScrollViewPositiong:scrollView];
}



-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self scrollViewDidEndDecelerating:scrollView];
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{
    if ([scrollView isEqual:_scrollView]) {
        _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width;
        
        
        //    UITableView *currentTable = _scrollTableViews[_currentPage];
        //    [currentTable reloadData];
        
        [self updateTableWithPageNumber:_currentPage];
        NSLog(@"scrollView的页数=%ld",(long)_currentPage);
        
        [self changeBackColorWithPage:_currentPage];
        
        return;
    }
    [self modifyTopScrollViewPositiong:scrollView];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([_scrollView isEqual:scrollView]) {
        CGRect frame = _slideView.frame;
        
        if (self.tabCount <= 6) {
            frame.origin.x = scrollView.contentOffset.x/_tabCount;
        } else {
            frame.origin.x = scrollView.contentOffset.x/6;
            
        }
        
        _slideView.frame = frame;
    }
    
}





#pragma mark -- talbeView的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    NSMutableArray *tempArray = _dataSource[_currentPage];
//    NSLog(@"tempArray.count = %lu",(unsigned long)_dataSource.count);
    //    return 10;
    return _dataSource.count;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(UITableViewCell *)tableView:tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BOOL nibsRegistered=NO;
    if (!nibsRegistered) {
        UINib *nib=[UINib nibWithNibName:@"SlideBarCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:@"SlideBarCell"];
        nibsRegistered=YES;
    }
    
    
    SlideBarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SlideBarCell"];
    if ([tableView isEqual:_scrollTableViews[_currentPage%2]]) {
    }
    cell.arrData = _dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.yuepianBtn setTitle:cell.pacUrl forState:UIControlStateHighlighted];
    [cell.yuepianBtn addTarget:self action:@selector(yuepian:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)yuepian:(UIButton*)sender{
    if (!my_AppDelegate.grabO) {
        return;
    }
    yuepianController *vc = [yuepianController new];
    NSString * zid = [sender titleForState:UIControlStateHighlighted];
    vc.yuepianURL = zid;
    if (!zid) {
        [SVProgressHUD showErrorWithStatus:@"暂无图片"];
    }else{
        vc.yuepianURL = zid;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SlideBarCell * cell =[tableView cellForRowAtIndexPath:indexPath];
    taskDetailsCVc *vc =[taskDetailsCVc new];
    shenhetuihuiController *vc2 =[shenhetuihuiController new];
    vc.typeStr = _nameStr;
    vc.zid = cell.zid;
    vc2.typeStr = _nameStr;
    vc2.zid = cell.zid;
    if (_currentPage == 2) {
        //已审核
        vc.odrStatus = @"已审核";
        vc.type = 7;
         [self.navigationController pushViewController:vc animated:YES];
    }else if (_currentPage == 1){
        //待审核
        vc.odrStatus = @"待审核";
        vc.type = 8;
         [self.navigationController pushViewController:vc animated:YES];
    }else if (_currentPage == 0){
        //未通过
        vc2.odrStatus = @"未通过";
        vc2.fromShenhe = YES;
        vc2.type = 10;
        [self.navigationController pushViewController:vc2 animated:YES];

    }
   
    NSLog(@"点击cell");
}
#pragma mark---------
- (void)viewDidLoad {
    [super viewDidLoad];
    _btnNameArr = [NSMutableArray arrayWithObjects:@"未通过",@"待审核",@"已审核",nil];
    _scrollTableViews = [[NSMutableArray alloc] init];
    _tabCount = _btnNameArr.count;
    _mViewFrame = self.view.frame;
    if (!_topViews) {
        _topViews =[[NSMutableArray alloc]init];
    }
    
    [self initScrollView];
    
    [self initTopTabs];
    
    [self initDownTables];
    
    [self loadData];
    
    [self refersh];
    
    [self creatNavRightBtn];
    
    [self initSlideView];
    
    [self changeBackColorWithPage:0];
    
    _myImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kong"]];
    _myImageView.frame = CGRectMake((ScreenWidth-134)/2, 100, 134, 134);
    [self.view addSubview:_myImageView];
    _myImageView.hidden = YES;
}
-(void)viewWillAppear:(BOOL)animated{
    if (my_AppDelegate.fromMessage) {
        UIButton * button = [[UIButton alloc]init];
        button.tag = 1;
        _currentPage = 1;
        [self tabButton:button];
    }
    [self loadData];
    [super viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
    my_AppDelegate.fromMessage = NO;
    [_myTableView1.mj_header endRefreshing];
    [_myTableView2.mj_header endRefreshing];
    [_myTableView1.mj_footer endRefreshing];
    [_myTableView2.mj_footer endRefreshing];
}
-(void)loadData{
    self.myTableView1.mj_footer.hidden = NO;
    self.myTableView2.mj_footer.hidden = NO;
    _pageNo1 = _pageNo2 = _pageNo3 = 1;
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    if (!_dataSource1) {
        _dataSource =[[NSMutableArray alloc]init];
    }
   
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    NSString *str =[NSString stringWithFormat:@"ordercompletedOrder"];
    switch (_currentPage) {
        case 0:
            //已接单
            [dic setValue:@2 forKey:@"status"];
            _nameStr =@"未通过";
            
            break;
        case 1:
            //已接单
            [dic setValue:@1 forKey:@"status"];
            _nameStr =@"待审核";
            break;
        case 2:
            //已接单
            [dic setValue:@3 forKey:@"status"];
            _nameStr =@"已审核";
            break;
        default:
            break;
    }
    [dic setValue:@10 forKey:@"size"];
    [dic setValue:@1 forKey:@"page"];
    [dic setValue:my_AppDelegate.token forKey:@"token"];
    [_dataSource removeAllObjects];
    int tabviewTag = _currentPage % 2;
    UITableView *reuseTableView = _scrollTableViews[tabviewTag];
    [reuseTableView reloadData];
    self.myTableView1.scrollEnabled = NO;
    self.myTableView2.scrollEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.myTableView1.scrollEnabled = YES;
        self.myTableView2.scrollEnabled = YES;
    });
    [NetWorking startPOSTRequestWithUrl:str parameters:dic logMes:_nameStr show:NO success:^(NSDictionary *data) {
        self.myTableView1.scrollEnabled = YES;
        self.myTableView2.scrollEnabled = YES;
        NSArray *arr = [data objectForKey:@"data"];
        switch (self->_currentPage) {
            case 0:
                //已接单
                self->_dataSource1 = [orderModel modelSformDics:arr];
                self->_dataSource = self->_dataSource1;
                
                break;
            case 1:
                //已接单
                self->_dataSource2 = [orderModel modelSformDics:arr];
                self->_dataSource = self->_dataSource2;
                
                break;
            case 2:
                //已接单
                self->_dataSource3 = [orderModel modelSformDics:arr];
                self->_dataSource = self->_dataSource3;
                
                break;
            default:
                break;
        }
        if (arr.count == 0||!arr) {
            self->_myImageView.hidden = NO;
        }else{
            self->_myImageView.hidden = YES;
            
        }
        if (arr.count==0||arr.count<10||!arr){
            dispatch_async(dispatch_get_main_queue(), ^{
//                [self.myTableView1 reloadData];
//                [self.myTableView2 reloadData];
                [self.myTableView1.mj_footer endRefreshing];
                [self.myTableView2.mj_footer endRefreshing];
                self.myTableView1.mj_footer.hidden = YES;
                self.myTableView2.mj_footer.hidden = YES;
                
                [self->_myTableView1.mj_footer setState:MJRefreshStateNoMoreData];
                [self->_myTableView2.mj_footer setState:MJRefreshStateNoMoreData];
            });
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            int tabviewTag = _currentPage % 2;
            UITableView *reuseTableView = _scrollTableViews[tabviewTag];
            [reuseTableView reloadData];
            [self.myTableView1.mj_header endRefreshing];
            [self.myTableView2.mj_header endRefreshing];
            self.myTableView1.scrollEnabled = YES;
            self.myTableView2.scrollEnabled = YES;
        });
    } failure:^(NSError *error) {
        
    }];
}
-(void)loadMoreData{
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    NSString *str =[NSString stringWithFormat:@"ordergetNotAcceptOrder"];
    _pageNo1++;
    _pageNo2++;
    _pageNo3++;
    switch (_currentPage) {
        case 0:
            //已接单
            [dic setValue:[NSString stringWithFormat:@"%d",_pageNo1] forKey:@"page"];
            
            break;
        case 1:
            //已接单
            [dic setValue:[NSString stringWithFormat:@"%d",_pageNo2] forKey:@"page"];
            break;
        case 2:
            //已接单
            [dic setValue:[NSString stringWithFormat:@"%d",_pageNo3] forKey:@"page"];
            break;
        default:
            break;
    }
    
    
    [dic setValue:@10 forKey:@"size"];
    
    [dic setValue:my_AppDelegate.token forKey:@"token"];
    
//    [self.dataSource removeAllObjects];
    
    self.myTableView1.scrollEnabled = NO;
    self.myTableView2.scrollEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.myTableView1.scrollEnabled = YES;
        self.myTableView2.scrollEnabled = YES;
    });
    [NetWorking startPOSTRequestWithUrl:str parameters:dic logMes:_nameStr show:NO success:^(NSDictionary *data) {
        self.myTableView1.scrollEnabled = YES;
        self.myTableView2.scrollEnabled = YES;
        NSArray *arr = [data objectForKey:@"data"];
        switch (self->_currentPage) {
            case 0:
                //已接单
                if (arr&&![arr isEqual:[NSNull null]]) {
                    for (NSDictionary * dic in arr) {
                        orderModel * model = [orderModel new];
                        [model setValuesForKeysWithDictionary:dic];
                        [self->_dataSource1 addObject:model];
                    }
                    self->_dataSource = [self->_dataSource1 mutableCopy];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        int tabviewTag = _currentPage % 2;
                        UITableView *reuseTableView = _scrollTableViews[tabviewTag];
                        [reuseTableView reloadData];
                        [self.myTableView1.mj_footer endRefreshing];
                        [self.myTableView2.mj_footer endRefreshing];
                    });
                }if (arr.count==0||arr.count<10){
                    dispatch_async(dispatch_get_main_queue(), ^{
                        int tabviewTag = _currentPage % 2;
                        UITableView *reuseTableView = _scrollTableViews[tabviewTag];
                        [reuseTableView reloadData];
                        [self.myTableView1.mj_footer endRefreshing];
                        [self.myTableView2.mj_footer endRefreshing];
                        self.myTableView1.mj_footer.hidden = YES;
                        self.myTableView2.mj_footer.hidden = YES;
                        [self->_myTableView1.mj_footer setState:MJRefreshStateNoMoreData];
                        [self->_myTableView2.mj_footer setState:MJRefreshStateNoMoreData];
                    });
                    
                }
                NSLog(@"_dataSource1==%lu",(unsigned long)self->_dataSource1.count);
                NSLog(@"_dataSource2==%lu",(unsigned long)self->_dataSource2.count);
                
                
                break;
            case 1:
                //已接单
                NSLog(@"_dataSource2==%lu",(unsigned long)self->_dataSource2.count);
                NSLog(@"_dataSource1==%lu",(unsigned long)self->_dataSource1.count);
                
                if (arr&&![arr isEqual:[NSNull null]]) {
                    for (NSDictionary * dic in arr) {
                        orderModel * model = [orderModel new];
                        [model setValuesForKeysWithDictionary:dic];
                        [self->_dataSource2 addObject:model];
                    }
                    self->_dataSource = [self->_dataSource2 mutableCopy];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        int tabviewTag = _currentPage % 2;
                        UITableView *reuseTableView = _scrollTableViews[tabviewTag];
                        [reuseTableView reloadData];
                        [self.myTableView1.mj_footer endRefreshing];
                        [self.myTableView2.mj_footer endRefreshing];
                    });
                }if (arr.count==0||arr.count<10){
                    dispatch_async(dispatch_get_main_queue(), ^{
                        int tabviewTag = _currentPage % 2;
                        UITableView *reuseTableView = _scrollTableViews[tabviewTag];
                        [reuseTableView reloadData];
                        [self.myTableView1.mj_footer endRefreshing];
                        [self.myTableView2.mj_footer endRefreshing];
                        self.myTableView1.mj_footer.hidden = YES;
                        self.myTableView2.mj_footer.hidden = YES;
                        
                        [self->_myTableView1.mj_footer setState:MJRefreshStateNoMoreData];
                        [self->_myTableView2.mj_footer setState:MJRefreshStateNoMoreData];
                    });
                    
                }
                
                break;
            case 2:
                //已接单
                NSLog(@"_dataSource2==%lu",(unsigned long)self->_dataSource2.count);
                NSLog(@"_dataSource1==%lu",(unsigned long)self->_dataSource1.count);
                
                if (arr&&![arr isEqual:[NSNull null]]) {
                    for (NSDictionary * dic in arr) {
                        orderModel * model = [orderModel new];
                        [model setValuesForKeysWithDictionary:dic];
                        [self->_dataSource3 addObject:model];
                    }
                    self->_dataSource = [self->_dataSource3 mutableCopy];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        int tabviewTag = _currentPage % 2;
                        UITableView *reuseTableView = _scrollTableViews[tabviewTag];
                        [reuseTableView reloadData];
                        [self.myTableView1.mj_footer endRefreshing];
                        [self.myTableView2.mj_footer endRefreshing];
                    });
                }if (arr.count==0||arr.count<10){
                    dispatch_async(dispatch_get_main_queue(), ^{
                        int tabviewTag = _currentPage % 2;
                        UITableView *reuseTableView = _scrollTableViews[tabviewTag];
                        [reuseTableView reloadData];
                        [self.myTableView1.mj_footer endRefreshing];
                        [self.myTableView2.mj_footer endRefreshing];
                        self.myTableView1.mj_footer.hidden = YES;
                        self.myTableView2.mj_footer.hidden = YES;
                        
                        [self->_myTableView1.mj_footer setState:MJRefreshStateNoMoreData];
                        [self->_myTableView2.mj_footer setState:MJRefreshStateNoMoreData];
                    });
                    
                }
                
                break;
            default:
                break;
        }
        
        
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)changeData{
    
    switch (_currentPage) {
        case 0:
            //已接单
            if (!_dataSource1.count) {
                [self loadData];
            }else{
                [self changeSouce];
            }
            break;
        case 1:
            //已接单
            if (_dataSource2.count==0||!_dataSource2) {
                [self loadData];
            }else{
                [self changeSouce];
            }
            break;
        case 2:
            //已接单
            if (_dataSource3.count==0||!_dataSource3) {
                [self loadData];
            }else{
                [self changeSouce];
            }
            break;
        default:
            break;
    }
    
}
-(void)changeSouce{
    //    _myImageView.hidden = YES;
    
    switch (_currentPage) {
        case 0:
            //已接单
            self->_dataSource = [self->_dataSource1 mutableCopy];
            
            break;
        case 1:
            //已接单
            self->_dataSource = [self->_dataSource2 mutableCopy];;
        case 2:
            //已接单
            self->_dataSource = [self->_dataSource2 mutableCopy];;
            break;
        default:
            break;
    }
    int tabviewTag = _currentPage % 2;
    UITableView *reuseTableView = _scrollTableViews[tabviewTag];
    [reuseTableView reloadData];
    [self.myTableView1.mj_header endRefreshing];
    [self.myTableView2.mj_header endRefreshing];
}
#pragma mark --rightBtn--
-(void)creatNavRightBtn{
    UIBarButtonItem *pulishButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];
    [pulishButton setTintColor:[UIColor whiteColor]];
    
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithTitle:@"titi" style:UITabBarSystemItemContacts target:self action:@selector(save:)];
//    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:saveButton ,pulishButton,nil]];
}
-(void)search{
    searchController * vc = [searchController new];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
